const app = getApp();
import grace from "../../../utils/grace.js";
import {Order} from '../order/order-model.js';

let order = new Order();

grace.page({
    /**
     * 页面的初始数据
     */
    data: {
        orderStates: ['all', 'grouping', 'delivery', 'close'],
        orderStateStrs: ['全部', '待成团', '拼团成功', '拼团失败'],
        currentIndex: 0,
        currentState: 'all',
        orderList: [],
        total: 0,
        page: 1,
        pagesize: 10
    },
    onLoad(e) {
        if (e.state) {
            this.$data.currentIndex = e.state;
            this.$data.currentState = this.$data.orderStates[e.state];
        }
    },
    onShow() {
        this.$data.page = 1;
        this.$data.orderList = [];
        this.getList();
    },
    statesTap(e) {
        this.$data.page = 1;
        this.$data.orderList = [];
        this.$data.currentIndex = e.currentTarget.dataset.index;
        this.$data.currentState = this.$data.orderStates[e.currentTarget.dataset.index];
        this.onShow();
    },
    orderDetail(e) {
        let orderId = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/pages/mall/order/detail?id=" + orderId
        })
    },
    getList() {
        let token = wx.getStorageSync('token');
        // token校验
        if (!token) {
            wx.removeStorageSync('userInfo');
            wx.navigateTo({
                url: '/pages/common/authorize/index'
            })
        } else {
            // 查询订单列表
            order.getOrderList({
                'token': wx.getStorageSync('token'), 'is_group': true, 'partner_id': wx.getStorageSync('partner_id'),
                'state': this.$data.currentState, 'page': this.$data.page, 'pagesize': this.$data.pagesize
            }, (data) => {
                this.$data.total = data.total;
                this.$data.page = this.$data.page + 1;
                this.$data.orderList = this.$data.orderList.concat(data.list);
            })
        }
    },
    onReachBottom() {
        if (this.$data.orderList.length < this.$data.total) {
            this.getList();
        }
    }
})